package com.shenganyuan.smac.common.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.text.TextUtils;
import android.view.View;

import com.amap.api.maps2d.model.LatLng;
import com.shenganyuan.smac.R;
import com.shenganyuan.smac.activity.ActMap;
import com.shenganyuan.smac.common.beans.BeanWorkLogSecond;
import com.shenganyuan.smac.common.beans.LocationInfo;
import com.shenganyuan.smac.common.data.AnyPrefUtil;
import com.shenganyuan.smac.common.module.imager.ActImgViewer;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class DataConverter {
    public static final String CODE_SPLIT_YEAR = "/";
    public static final String CODE_SPLIT_TIME = ":";

    /**
     * @param givenStr 后台传递20161024155052
     * @return
     */
    public static String getDateStr(String givenStr) {
        if (TextUtils.isEmpty(givenStr)) {
            return "";
        }
        StringBuffer stringBuffer = new StringBuffer();
        try {
            if (givenStr.length() >= 8) {
                //年月日
                String year = givenStr.substring(0, 4);
                String month = givenStr.substring(4, 6);
                String day = givenStr.substring(6, 8);
                stringBuffer.append(year);
                stringBuffer.append(CODE_SPLIT_YEAR);
                stringBuffer.append(month);
                stringBuffer.append(CODE_SPLIT_YEAR);
                stringBuffer.append(day);
                if (givenStr.length() >= 12) {
                    stringBuffer.append(" ");
                    String hour = givenStr.substring(8, 10);
                    stringBuffer.append(hour);
                    stringBuffer.append(CODE_SPLIT_TIME);
                    String minute = givenStr.substring(10, 12);
                    stringBuffer.append(minute);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stringBuffer.toString();
    }

    /**
     * 从字符串获取坐标
     *
     * @param gps 传入为空则返回当前坐标
     * @return
     */
    public static LatLng fromStrToLatLng(String gps) {
        LatLng latLng = null;
        if (TextUtils.isEmpty(gps)) {
            LocationInfo locationInfo = AnyPrefUtil.getLocationInfo();
            if (locationInfo != null) {
                latLng = new LatLng(Double.valueOf(locationInfo.latitude), Double.valueOf(locationInfo.longitude));
            }
        } else {
            if (gps.contains(";")) {
                String[] gpsStr = gps.split(";");
                double longitude = Double.valueOf(gpsStr[0]);
                double latitude = Double.valueOf(gpsStr[1]);
                latLng = new LatLng(latitude, longitude);
            }
        }
        return latLng;
    }

    /**
     * 从字符串获取图片地址
     *
     * @param imgStr 传入为空则返回当前坐标
     * @return
     */
    public static List<String> fromImgStrsToImg(String imgStr) {
        List<String> list = new ArrayList<>();
        if (!TextUtils.isEmpty(imgStr)) {
            if (imgStr.contains(";")) {
                String[] array = imgStr.split(";");
                list.addAll(Arrays.asList(array));
            }
        }
        return list;
    }

    /**
     * 根据日志的类型获取文字
     *
     * @param workLogSecond
     * @return
     */
    public static String getLogType(BeanWorkLogSecond workLogSecond) {
        String result = "";
        switch (workLogSecond.getType()) {
            case BeanWorkLogSecond.TYPE_LEAVE:
                result = "假";
                break;
            case BeanWorkLogSecond.TYPE_SIGN:
                result = "签";
                break;
            case BeanWorkLogSecond.TYPE_XCXX:
                result = "巡";
                break;
        }
        return result;
    }

    /**
     * 根据日志的类型获取文字背景
     *
     * @param workLogSecond
     * @return
     */
    public static int getLogTypeBgColor(BeanWorkLogSecond workLogSecond) {
        int intColor = -1;
        switch (workLogSecond.getType()) {
            case BeanWorkLogSecond.TYPE_LEAVE:
                intColor = R.color.color_work_log_leave;
                break;
            case BeanWorkLogSecond.TYPE_SIGN:
                intColor = R.color.color_work_log_sign;
                break;
            case BeanWorkLogSecond.TYPE_XCXX:
                intColor = R.color.color_work_log_xcxx;
                break;
        }
        return intColor;
    }

    /**
     * 打开地图
     *
     * @param context
     * @param latLngFrom
     * @param latLngTo
     * @param type
     */
    public static void pickMap(Context context, LatLng latLngFrom, LatLng latLngTo, int type) {
        if (latLngTo == null) {
            return;
        }
        switch (type) {
            case 0:
                OpenMapApp.goToNaviActivity(context, "amap", "", latLngTo.latitude + "", latLngTo.longitude + "", "0", "0");
                break;
            case 1:
                OpenMapApp.goToBaiduMap(context, latLngTo.latitude + "", latLngTo.longitude + "", latLngFrom.latitude + "", latLngFrom.longitude + "");
                break;
            default:
                break;
        }
    }

    /**
     * 根据传入的经纬度打开地图
     *
     * @param longitude
     * @param latitude
     */
    public static void openSdkMap(Context context, double latitude, double longitude) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        intent.setClass(context, ActMap.class);
        bundle.putDouble("longitude", longitude);
        bundle.putDouble("latitude", latitude);
        intent.putExtras(bundle);
        context.startActivity(intent);

    }

    /**
     * 打开图片详情页
     *
     * @param mContext
     * @param imgUrls
     * @param archerView
     */
    public static void openPic(Context mContext, int index, List<String> imgUrls, View archerView) {
        if (mContext == null || imgUrls == null || imgUrls.size() == 0) {
            return;
        }
        Intent intent = new Intent();
        intent.setClass(mContext, ActImgViewer.class);
        Bundle bundle = new Bundle();
        bundle.putInt(ActImgViewer.EXTRA_IMAGE_INDEX, index);
        bundle.putSerializable(ActImgViewer.EXTRA_IMAGE_URLS, (Serializable) imgUrls);
        intent.putExtras(bundle);
        ActivityCompat.startActivity(mContext, intent,
                ActivityOptionsCompat.makeSceneTransitionAnimation((Activity) mContext, archerView, "translate_view").toBundle());
    }

}
